package com.itita.ww2.data;

import com.smartfoxserver.v2.entities.data.ISFSArray;
import com.smartfoxserver.v2.entities.data.ISFSObject;

public class AllianceConfig {
	public static boolean isLoaded = false;
	public static final String disbandPrefix = "al_disband:";
	public static final String transferPrefix = "al_transfer:";
	// 
	public static final int pageCount = 20;
	// 
	private static ISFSObject allianceCfg; // 联盟配置
	// private static ISFSObject allianceBattleCfg; // 盟战配置

	public static void init(ISFSObject data) {
		allianceCfg = data.getSFSObject("alliance");
		// allianceBattleCfg = data.getSFSObject("alliance_battle");
	}

	/**
	 * 创建联盟限制等级是否达到
	 */
	public static final boolean isCreateLvLimit(int lv) {
		return lv >= allianceCfg.getInt("openLv");
	}

	/**
	 * 获取创建联盟所需资源
	 */
	public static final ISFSObject getCreateResource() {
		return allianceCfg.getSFSObject("createRes");
	}

	/**
	 * 获取联盟初始人数
	 */
	public static final int getDefaultMember() {
		return allianceCfg.getInt("defaultMember");
	}

	/**
	 * 获取联盟最大人数
	 */
	public static final int getMaxMember() {
		return allianceCfg.getInt("maxMember");
	}

	/**
	 * 获取加入联盟的CD<br>
	 * 只有在退出后才有cd
	 */
	public static final int getJoinCD() {
		return allianceCfg.getInt("quitCD");
	}

	/**
	 * 获取加盟请求持续时间
	 */
	public static final int getJoinRequestTime() {
		return allianceCfg.getInt("joinRequestTime");
	}

	/**
	 * 获取解散联盟持续时间
	 */
	public static final int getDisbandTime() {
		return allianceCfg.getInt("disbandTime");
	}

	/**
	 * 权限类别
	 * @author NorthLan
	 */
	public static enum PermType {
		KICKUSER, // T人
		APPOINT, // 任命
		MODIFY_JOINLIMIT, // 修改加入参数
		MODIFY_DECLAR, // 修改宣言
		MODIFY_NOTICE, // 修改公告
		MODIFY_NAME, // 修改名字
		JOIN_RECEIPT, // 回执加入请求
		DISBAND, // 解散
		RECALL, // 罢免
		TRANSFER, // 转让
		;

		@Override
		public String toString() {
			return super.toString().toUpperCase();
		}
	}

	/**
	 * 对应职位 是否具有权限
	 */
	public static final boolean hasPermission(int role, int targetRole, PermType permType) {
		ISFSObject roleObj = allianceCfg.getSFSObject("role");
		ISFSObject permObj = null;
		for (String key : roleObj.getKeys()) {
			ISFSObject obj = roleObj.getSFSObject(key);
			int objRole = obj.getInt("role");
			if (objRole != role) {
				continue;
			}
			permObj = obj.getSFSObject("perm");
		}
		if (permObj == null) {
			return false;
		}
		if (!permObj.containsKey(permType.toString())) {
			return false;
		}
		ISFSArray permArr = permObj.getSFSArray(permType.toString());
		if (permArr.size() <= 0) {
			return true;
		}
		return permArr.contains(targetRole);
	}

	public static enum AlRoleType {
		LEADER, // 盟主
		DEPUTYLEADER, // 副盟主
		COMMANDER, // 指挥官
		DIPLOMAT, // 外交官
		QUARTERMASTER, // 军需官
		// 
		SERGEANCY, // 士官
		SOLDIER, // 士兵
		;
		@Override
		public String toString() {
			return super.toString().toUpperCase();
		}

		public static AlRoleType valueOf(int value) {
			for (AlRoleType type : values()) {
				if (getRole(type) == value) {
					return type;
				}
			}
			return SOLDIER;
		}
	}

	public static final int getRole(AlRoleType roleType) {
		return allianceCfg.getSFSObject("role").getSFSObject(roleType.toString()).getInt("role");
	}

	/**
	 * 获取职位数量
	 * @param level 当前联盟等级
	 */
	public static final int getRoleCount(AlRoleType roleType, int level) {
		ISFSObject obj = allianceCfg.getSFSObject("role").getSFSObject(roleType.toString());
		if (!obj.containsKey("count")) {
			return Integer.MAX_VALUE;
		}
		return obj.getSFSArray("count").getInt(level - 1);
	}

	/**
	 * 获取升级需求贡献
	 * @param level 当前联盟等级
	 */
	public static final int getUpgradeContribution(int level) {
		ISFSArray arr = allianceCfg.getSFSArray("lv_contribution");
		if (level >= getMaxLv()) {
			return Integer.MAX_VALUE;
		}
		return arr.getInt(level);
	}

	/**
	 * 获取最大联盟等级
	 */
	public static final int getMaxLv() {
		return allianceCfg.getInt("maxLv");
	}

	/**
	 * 获取比值
	 */
	public static final float getContributeRatio(String resKey) {
		return allianceCfg.getSFSObject("contributeRatio").getFloat(resKey);
	}

	/**
	 * 获取职位最低贡献
	 */
	public static final int getMinCTB(AlRoleType roleType) {
		ISFSObject roleObj = allianceCfg.getSFSObject("role").getSFSObject(roleType.toString());
		if (!roleObj.containsKey("minCTB")) {
			return Integer.MAX_VALUE;
		}
		return allianceCfg.getSFSObject("role").getSFSObject(roleType.toString()).getInt("minCTB");
	}

	/**
	 * 获取转让时间条件
	 */
	public static final int getTransferTimeCondition() {
		return allianceCfg.getSFSObject("transfer").getInt("condition");
	}

	/**
	 * 转让持续时间
	 */
	public static final int getTransferDuration() {
		return allianceCfg.getSFSObject("transfer").getInt("transferTime");
	}
}
